package com.msw.rest.service.jersey;

import com.msw.rest.bean.rsp.ErrorRsp;
import com.msw.rest.common.util.JsonUtils;
import com.msw.rest.model.Stu;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * @Author hadoop
 * @Date 2015/11/14.
 */
@Path("/users")
public class UserResources {

    @GET
    @Path("/{id}")
    @Produces({"application/json"})
    @Consumes({"application/json"})
    public Stu getUser(@PathParam("id")String id)
    {
        System.out.println(id);
        if(id.equals("123"))
        {

            throw new NotFoundException(Response
                .status(Response.Status.NOT_FOUND)
                .entity(JsonUtils.formatObject(new ErrorRsp(40404, "user not found")))
                .build());
        }
        return new Stu("123", "Mike");
    }

    /**
     * get request receive json data
     *
     * @param s
     * @return
     */
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Stu getUserByJson(@QueryParam("stu")String s)
    {
        Stu stu = JsonUtils.readToObject(s, Stu.class);
        System.out.println(stu);
        return stu;
    }

    @POST
    @Consumes({"application/json"})
    @Produces({"text/plain"})
    public String postUser(Stu stu)
    {
        System.out.println(stu);
        return "save sutdent success";
    }

}
